package com.alliky.classify.ui.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.alliky.classify.BR;
import com.alliky.classify.R;
import com.alliky.core.base.viewmodel.ToolbarViewModel;
import com.alliky.core.binding.command.BindingAction;
import com.alliky.core.binding.command.BindingCommand;
import com.alliky.core.bus.RxMessager;
import com.alliky.core.bus.SingleLiveEvent;
import com.alliky.core.data.DataRepository;
import com.alliky.core.entity.vo.CategoryVO;
import com.alliky.core.entity.vo.GoodsVO;
import com.alliky.core.net.IDisposableObserver;
import com.alliky.core.util.KLog;
import com.alliky.core.util.RxUtils;

import java.util.List;
import java.util.WeakHashMap;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * @Description TODO
 * @Author wxianing
 * @Date 2020-11-15
 */
public class ClassifyViewModel extends ToolbarViewModel<DataRepository> {

    public static final String TOKEN_CLASSIFYVIEWMODEL_REFRESH = "token_classifyviewmodel_refresh";

    private int pageNum = 1;
    private int pageSize = 12;

    public ClassifyViewModel(@NonNull Application application) {
        super(application);
        RxMessager.getDefault().register(ClassifyViewModel.this, "1111", String.class, s -> KLog.d(">>>>>>>>", s));
    }

    public void initToolbar() {
        setBackIconVisible(View.GONE);
        setTitleText("分类");
    }

    //给RecyclerView添加ObservableList
    public ObservableList<ClassifyItemLeftViewModel> observableLeftList = new ObservableArrayList<>();
    public ObservableList<ClassifyItemContentViewModel> observableContentList = new ObservableArrayList<>();
    //给RecyclerView添加ItemContentBinding
    public ItemBinding<ClassifyItemLeftViewModel> itemLeftBinding = ItemBinding.of(BR.viewModel, R.layout.item_category_left_layout);
    public ItemBinding<ClassifyItemContentViewModel> itemContentBinding = ItemBinding.of(BR.viewModel, R.layout.item_category_content_layout);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent finishRefresh = new SingleLiveEvent<>();
        //上拉加载完成
        public SingleLiveEvent finishLoadMore = new SingleLiveEvent<>();
    }

    //下拉刷新
    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            pageNum = 1;
            getGoodsData();
        }
    });
    //上拉加载
    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            pageNum++;
            getGoodsData();
        }
    });

    public void getCategoryData() {
        WeakHashMap param = new WeakHashMap();
        param.put("1", 1);
        mRepository.getCategory(param)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))// 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())// 线程调度
                .compose(RxUtils.exceptionTransformer())// 网络错误的异常转换
                .doOnSubscribe(this)
                .subscribe(new IDisposableObserver<List<CategoryVO>>(this) {
                    @Override
                    protected void onResult(List<CategoryVO> categoryVOS) {
                        for (CategoryVO categoryVO : categoryVOS) {
                            ClassifyItemLeftViewModel viewModel = new ClassifyItemLeftViewModel(ClassifyViewModel.this, categoryVO);
                            observableLeftList.add(viewModel);
                        }
                    }
                });
    }

    public void getGoodsData() {
        WeakHashMap<String, Object> param = new WeakHashMap<>();
        param.put("pageNum", pageNum);
        param.put("pageSize", pageSize);
//        param.put("keyword", "");
        mRepository.getHotGoods(param)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))// 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())// 线程调度
                .compose(RxUtils.exceptionTransformer())// 网络错误的异常转换
                .doOnSubscribe(this)
                .subscribe(new IDisposableObserver<List<GoodsVO>>(null) {
                    @Override
                    protected void onResult(List<GoodsVO> goodsVOS) {

                        if (pageNum == 1) {
                            observableContentList.clear();
                            uc.finishRefresh.call();
                        } else {
                            uc.finishLoadMore.call();
                        }

                        for (GoodsVO goodsVO : goodsVOS) {
                            ClassifyItemContentViewModel viewModel = new ClassifyItemContentViewModel(ClassifyViewModel.this, goodsVO);
                            observableContentList.add(viewModel);
                        }
                    }
                });
    }
}
